<?php

namespace App\Http\Controllers\OA;

use App\Http\Controllers\Constant\Constant;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Logic\FinanceLogic;
use App\Http\Controllers\Logic\ShenPiLogLogic;
use App\Http\Requests\AddFinanceRequest;
use App\Model\OAFiles;
use App\Model\OAFinance;
use App\Model\OAFinanceClv;
use App\Model\OAPositionMapping;
use App\Model\OAShenPiLog;
use App\Model\OAWork;
use Illuminate\Http\Request;
use App\Model\OATicket;
use Illuminate\Support\Arr;

class FinanceController extends Controller
{
    //计算差旅费用
    public function getClvFee(Request $request)
    {
        $rules = [
            'city_level'=>'sometimes|numeric|in:1,2,3,4|nullable',
            'date'=>'required|array',
            'staff_list'=>'sometimes|array|nullable',
            'staff_list.*'=>'required|string',
        ];

        try{
            $this->verify($request->all(),$rules,['date.required'=>'出差日期不能为空']);

            $logic = new FinanceLogic();

            $result = $logic->getClvFee($request->all());

            ret_json('ok','ok',$result);

        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //获取可申请项目
    public function getApplyProject(Request $request)
    {
            $rules = ['type'=>'required|numeric'];

            try{
                $params = $this->mergeRequest($request);

                $this->verify($params,$rules);

                $list = (new FinanceLogic())->getApplyProject($params);

                ret_json('ok','ok',$list);

            }catch (\Exception $e){
                ret_json('err',$e->getMessage());
            }
    }

    //获取财务模块财务详情
    public function getProjectBase(Request $request)
    {
        $rules = [
            'id'=>'required|exists:project_lx,id',
            'type'=>'required|numeric|in:1,2,3,4,5,6,7,9,8'
        ];

        try{
        $this->verify($request->all(),$rules);
        $params = $request->all();
        $params['is_need_log'] = 1;
        $info = (new FinanceLogic())->getProjectBase($params);

        ret_json('ok','ok',$info);

        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }
    //添加财务申请
    public function addFinanceData(AddFinanceRequest $request)
    {
        try{
            $params = $this->mergeRequest($request);
            $result = (new FinanceLogic())->addFinanceData($request);
            $msg = $params['is_save'] ? '保存' : '提交';
           ret_json('ok',$msg .'成功',['cwid'=>$result]);

        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }
    //财务列表
    public function getList(Request $request)
    {
        $rules = [
            'name'=>'sometimes|nullable|string',//项目名称
            'title'=>'sometimes|nullable|string',//财务标题
            'created_user'=>'sometimes|nullable|array',
            'status'=>'present|nullable|in:0,1,2',
            'date'=>'sometimes|nullable|array',
            'batch_id'=>'sometimes|nullable|string',
            'paytype'=>'sometimes|nullable|in:1,2,3,4,5,6,7,8,9',
            'page'=>'required|min:1|numeric',
            'page_size'=>'required|min:10|numeric',
        ];

        try{
            $params = $this->mergeRequest($request);
            $this->verify($params,$rules);
            $list = (new FinanceLogic())->getList($params);
            ret_json('ok','请求成功',$list);

        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }
    //获取财务详情
    public function getFinanceDetail(Request $request){
        $rules = ['id'=>'required|exists:finance,id'];
            $params = $this->mergeRequest($request);
            $this->verify($params,$rules);
            $roleid = $request->roleid;
            $info = (new FinanceLogic())->getFinanceDetail($params);
            $clvList = $info->clv_list ?? [];
            $extend = $this->getInfoExtend($info,$roleid);
            if($clvList){
                $clvList = array_map(function ($item) use ($extend){
                    $item['disabled'] = $item['is_del'] ? true : Arr::get($extend,'disable');
                    return $item;
                },$clvList);
                $info->clv_list = $clvList;
            }
            $info->btn = $extend['btn'];
            $info->disabled = $extend['disable'];
            $info->is_hidden = $extend['hidden'];
            $info->show_hzd = $info->roleid == Constant::ROLE_SHEN_JI && $roleid == Constant::ROLE_SHEN_JI ? 1 : 0;
            $info->is_show_btn = $extend['is_show_btn'];
            $info->is_upload_file = Arr::get($extend,'is_upload_file');
            ret_json('ok','ok',$info);
    }

    //编辑财务
    public function editFinance(Request $request,OAFinance $finance)
    {
        $rules = [
            'id'=>'required|exists:finance,id',
            'finance'=>'required',
            'is_save'=>'required|in:0,1'
        ];

        try{
            $params = $this->mergeRequest($request);
            $this->verify($params,$rules);
            $result = (new FinanceLogic())->editFinance($request,$finance);
            $action = Arr::get($params,'finance.btn','提交');
            $msg = $params['is_save'] ? '保存成功' : $action . '成功';
            //dd($msg);
            ret_json('ok',$msg);

        }catch (\Exception $e){
            //dump($e);
            ret_json('err',$e->getMessage());
        }
    }

    //获取发票列表
    public function getTicketList(Request $request)
    {
        try{

            $rules = [
                'account'=>'sometimes',
                'short_name'=>'sometimes',
                'status'=>'sometimes',
                'bank'=>'sometimes',
                'page'=>'sometimes|numeric|min:1',
                'page_size'=>'sometimes|numeric|min:10',
                'is_list'=>'sometimes|numeric|in:0,1'
            ];

            $this->verify($request->all(),$rules);

            $list = (new FinanceLogic())->getTicketList($request->all());

            $ticket = config('oa.ticket');

            $response = [
                'list'=>$list,
                'category'=>$ticket['category'],
                'type'=>$ticket['type'],
            ];
            ret_json('ok','ok',$response);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //获取发票详情
    public function getTicketInfo(Request $request)
    {
        try{
           $rules = ['id'=>'required|exists:ticket,id'];

           $this->verify($request->all(),$rules);

           $info = (new FinanceLogic())->getTicketInfo($request->all());

           ret_json('ok','ok',$info);

        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //添加发票
    public function addTicket(Request $request)
    {
        try{
            $rules = [
                'id'=>'required|numeric',
                'account'=>'required|string',
                'name'=>'required|string',
                'short_name'=>'required|string',
                'address'=>'required|string',
                'bank'=>'required|string',
                'batch_id'=>'required|string',
                'bank_number'=>'required|string',
                'mobile'=>'required|string',
                'status'=>'required|numeric|in:0,1',
            ];
            $params = $request->all();
            $this->verify($params,$rules);
            $params = $this->mergeRequest($request);
            $result = (new FinanceLogic())->addTicket($params);
            ret_json('ok','编辑客户成功');

        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //获取差旅数据
    public function getClvData(Request $request)
    {
        try{
            $rules = ['id'=>'required|exists:finance,id'];
            $this->verify($request->all(),$rules);
            $data = (new FinanceLogic())->getClvData($request->all());
            ret_json('ok','ok',$data);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //获取已审批记录
    public function getApplyLog(Request $request){
        try{
            $rules = [
                'type'=>'required|numeric|in:1,2,3,4,5,6,7,8,9',
                'xmid'=>'required|numeric|exists:project_lx,id',
                'ysid'=>'numeric|sometimes|nullable',
            ];

            $this->verify($request->all(),$rules);
            $logList = (new FinanceLogic())->getApplyLog($request->all());
            ret_json('ok','ok',$logList);

        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }
    //删除客户
    public function delTicket(OATicket $ticket)
    {
        $ticket->status = 1;
        $ticket->save();
        ret_json('ok','ok',$ticket->id);
    }

    public function deleteFinance(Request $request,OAFinance $finance)
    {
        $mapPosition = OAPositionMapping::getPositionMapping($finance->roleid);
       if($request->uid != $finance->uid) throw new \Exception("用户无权限");
       if($request->roleid != $finance->create_role) throw new \Exception("职位无权限");
       if(!in_array(roleid(),$mapPosition)) throw new \Exception('当前项目正在审批');
       if($finance->paytype == Constant::PAY_TYPE_CLV) $finance->clvs()->delete();
       $finance->delete();
       return $this->success('删除成功',$finance->id);
    }

    //添加回执单
    public function addFinanceHzd(Request $request,OAFinance $finance)
    {
        if($request->roleid != Constant::ROLE_SHEN_JI) throw new \Exception('无权限添加回执单');
        $files = $request->post('files',[]);
        if(empty($files)) throw new \Exception("请添加文件");
        $this->saveFileList($finance->id,$files,FILE_HZD,$finance->work_id);
        return $this->success('添加成功');

    }

    //删除回执单
    public function delFinanceHzd(Request $request,OAFiles $file)
    {
        $file->delete();
        return $this->success("删除成功");
    }

    //删除差旅
    public function delClv(Request $request, OAFinanceClv $clv)
    {
        $clv->is_del = Constant::STATUS_ON;
        $clv->save();
        return $this->success("删除成功");
    }


}
